package nickthissen.iracing.common;

import nickthissen.iracing.datamodels.Result;
import nickthissen.iracing.networking.Networking;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import org.apache.commons.lang3.exception.ExceptionUtils;

public class Error 
{
	public Exception exception;
	public String message;
	public String detailMessage;
	
	public Error(String message, String detailMessage, Exception ex)
	{
		this(message, detailMessage);
		this.exception = ex;
	}
	
	public Error(String message, String detailMessage)
	{
		this(message);
		this.detailMessage = detailMessage;
	}
	
	public Error(String message)
	{
		this.message = message;
	}
	
	public Error()
	{
		
	}
	
	public void report(Context context)
	{
		this.show(context);
		this.log(context);
	}
	
	public void show(Context context)
	{
		Toast.makeText(context, message, Toast.LENGTH_LONG).show();
	}
	
	public void log(Context context)
	{
		Log.e("AutoLog", this.message);
		Log.e("AutoLog", "(Detail): " + this.detailMessage);

        ServiceLogging.logError(context, this);
	}
	
	public static Error fromException(String message, Exception ex)
	{
		Error error = new Error();
		error.exception = ex;
		error.message = message;
		error.detailMessage = ExceptionUtils.getStackTrace(ex);
		return error;
	}
	
	public static Error fromResult(Result result)
	{
		Error error = new Error();
		if (result.Object instanceof Exception) error.exception = (Exception) result.Object;
		error.message = result.Message;
		error.detailMessage = result.DetailMessage;
		return error;
	}
}
